﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SVM {
    public class TwoClassEvaluator {
        public int TP { get; private set; }
        public int TN { get; private set; }
        public int FP { get; private set; }
        public int FN { get; private set; }
        public double Sensitivity { get; private set; }
        public double Specificity { get; private set; }
        public double Accuracy { get; private set; }
        public double MCC { get; private set; }


        public TwoClassEvaluator(int tp, int tn, int fp, int fn) {
            TP = tp;
            TN = tn;
            FP = fp;
            FN = fn;
            Calculate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="target">Predict labels</param>
        /// <param name="problem">Original labels</param>
        public TwoClassEvaluator(double[] target, double[] problem)
            //: this(0, 0, 0, 0)
        {
            TP = TN = FP = FN = 0;
            if (target == null || problem == null || target.Length != problem.Length)
                throw new ArgumentException("Please check dataset format !");
            for (int i = 0; i < target.Length; i++) {
                if (target[i] == problem[i]) {
                    if (target[i] == 1) TP++;
                    else TN++;
                }
                else {
                    if (target[i] == 1) FP++;
                    else FN++;
                }
            }
            Calculate();
        }

        private void Calculate() {
            int sum = TP + TN + FP + FN;
            Sensitivity = 1.0 * TP / (TP + FN);
            Specificity = 1.0 * TN / (TN + FP);
            Accuracy = 1.0 * (TP + TN) / sum;
            MCC = 1.0 * (TP * TN - FP * FN) /
              Math.Sqrt(TP + FP) /
              Math.Sqrt(TP + FN) /
              Math.Sqrt(TN + FP) /
              Math.Sqrt(TN + FN);
        }
    }
}
